package com.fly.lambda.controller;

import com.fly.lambda.domain.Channel;
import com.fly.lambda.service.ChannelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Fly on 2016/7/14.
 */
@Controller
@RequestMapping("/channel")
public class ChannelCtrl {

    @Autowired
    ChannelService channelService;

    @RequestMapping("/list")
    public String toChannelList(
    ){
        return "manage/channel/list";
    }

    @RequestMapping("/getChannelList")
    @ResponseBody
    public Map<String, Object> getChannelList(
            @RequestParam(value = "pageSize", required = false, defaultValue = "10")int pageSize,
            @RequestParam(value = "curPage", required = false, defaultValue = "1")int curPage
    ){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("channelList", channelService.getList());
        return map;
    }


    @RequestMapping("/form")
    public String toChannelForm(
            @RequestParam(value = "channelId", required = false, defaultValue = "")String channelId,
            ModelMap model
    ){
        model.addAttribute("channelId", channelId);
        return "/manage/channel/form";
    }

    @RequestMapping("/getById")
    @ResponseBody
    public Map<String, Object> getById(
            @RequestParam(value = "channelId", required = false, defaultValue = "")String channelId
    ){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("channel", channelService.getById(channelId));
        return map;
    }

    @RequestMapping("/saveChannel")
    @ResponseBody
    public String saveChannel(
            @RequestParam(value = "channelId", required = false, defaultValue = "")String channelId,
            @RequestParam(value = "channelName", required = false, defaultValue = "")String channelName,
            @RequestParam(value = "channelDescription", required = false, defaultValue = "")String channelDescription,
            @RequestParam(value = "channelNum", required = false, defaultValue = "0")int channelNum,
            @RequestParam(value = "parentChannelId", required = false, defaultValue = "")String parentChannelId,
            @RequestParam(value = "channelSeoTitle", required = false, defaultValue = "")String channelSeoTitle,
            @RequestParam(value = "channelSeoKeywords", required = false, defaultValue = "")String channelSeoKeywords,
            @RequestParam(value = "channelSeoDescription", required = false, defaultValue = "")String channelSeoDescription
    ){
        Channel channel = new Channel();
        channel.setChannelId(channelId);
        channel.setChannelName(channelName);
        channel.setChannelDescription(channelDescription);
        channel.setChannelNum(channelNum);
        if (StringUtils.isNotBlank(parentChannelId)){
            channel.setParentChannel(new Channel(parentChannelId));
        }
        channel.setChannelSeoTitle(channelSeoTitle);
        channel.setChannelSeoKeywords(channelSeoKeywords);
        channel.setChannelSeoDescription(channelSeoDescription);
        channelService.saveChannel(channel);
        return "SUCCESS";
    }

}
